package cn.oesoft.tennissystem.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.oesoft.tennissystem.model.entity.TennisRacket;
import cn.oesoft.tennissystem.model.entity.Comment;
import cn.oesoft.tennissystem.model.responseData.TennisRacketResponseData;
import cn.oesoft.tennissystem.model.responseData.StaticticsBo;
import cn.oesoft.tennissystem.service.ITennisRacketService;
import cn.oesoft.tennissystem.service.ISiteService;
/**
 * @Classname AdminController
 * @Description 后台管理模块
 * @Date 2019-3-14 10:39
 * @Created by CrazyStone
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private ISiteService siteServiceImpl;
    @Autowired
    private ITennisRacketService tennisRacketServiceImpl;

    // 管理中心起始页
    @GetMapping(value = {"", "/index"})
    public String index(HttpServletRequest request) {
        // 获取最新的5篇博客、评论以及统计数据
        List<TennisRacket> tennisRackets = siteServiceImpl.recentTennisRackets(5);
        List<Comment> comments = siteServiceImpl.recentComments(5);
        StaticticsBo staticticsBo = siteServiceImpl.getStatistics();
        // 向Request域中存储数据
        request.setAttribute("comments", comments);
        request.setAttribute("tennisRackets", tennisRackets);
        request.setAttribute("statistics", staticticsBo);
        return "back/index";
    }

    // 向文章发表页面跳转
    @GetMapping(value = "/tennisRacket/toEditPage")
    public String newTennisRacket( ) {
        return "back/tennisRacket_edit";
    }
    // 发表文章
    @PostMapping(value = "/tennisRacket/publish")
    @ResponseBody
    public TennisRacketResponseData publishTennisRacket(TennisRacket tennisRacket) {
        if (StringUtils.isBlank(tennisRacket.getCategories())) {
            tennisRacket.setCategories("默认分类");
        }
        try {
            tennisRacketServiceImpl.publish(tennisRacket);
            logger.info("文章发布成功");
            return TennisRacketResponseData.ok();
        } catch (Exception e) {
            logger.error("文章发布失败，错误信息: "+e.getMessage());
            return TennisRacketResponseData.fail();
        }
    }
    // 跳转到后台文章列表页面
    @GetMapping(value = "/tennisRacket")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "count", defaultValue = "10") int count,
                        HttpServletRequest request) {
        //PageInfo<TennisRacket> pageInfo = tennisRacketServiceImpl.selectTennisRacketWithPage(page, count);
        PageInfo<TennisRacket> pageInfo = tennisRacketServiceImpl.getWithPage(page, count);
        request.setAttribute("tennisRackets", pageInfo);
        return "back/tennisRacket_list";
    }

    // 向文章修改页面跳转
    @GetMapping(value = "/tennisRacket/{id}")
    public String editTennisRacket(@PathVariable("id") String id, HttpServletRequest request) {
        //TennisRacket tennisRacket = tennisRacketServiceImpl.selectTennisRacketWithId(Integer.parseInt(id));
        TennisRacket tennisRacket = tennisRacketServiceImpl.getById(Integer.parseInt(id));
        request.setAttribute("contents", tennisRacket);
        request.setAttribute("categories", tennisRacket.getCategories());
        return "back/tennisRacket_edit";
    }

    // 文章修改处理
    @PostMapping(value = "/tennisRacket/modify")
    @ResponseBody
    public TennisRacketResponseData modifyTennisRacket(TennisRacket tennisRacket) {
        try {
            //tennisRacketServiceImpl.updateTennisRacketWithId(tennisRacket);
            tennisRacketServiceImpl.updateById(tennisRacket);
            logger.info("文章更新成功");
            return TennisRacketResponseData.ok();
        } catch (Exception e) {
            logger.error("文章更新失败，错误信息: "+e.getMessage());
            return TennisRacketResponseData.fail();
        }
    }

    // 文章删除
    @PostMapping(value = "/tennisRacket/delete")
    @ResponseBody
    public TennisRacketResponseData delete(@RequestParam int id) {
        try {
            //tennisRacketServiceImpl.deleteTennisRacketWithId(id);
            tennisRacketServiceImpl.deleteById(id);
            logger.info("文章删除成功");
            return TennisRacketResponseData.ok();
        } catch (Exception e) {
            logger.error("文章删除失败，错误信息: "+e.getMessage());
            return TennisRacketResponseData.fail();
        }
    }



}

